<template>
  <el-select v-model="model" filterable :placeholder="placeholder" @change="change">
    <el-option v-for="item in options"
               :key="item.value"
               :label="item.text"
               :value="item.value">
    </el-option>
  </el-select>
</template>

<script>
import {getEnterWarehouse} from '@/utils/selectOptions'
export default {
  name: 'SelectWarehouseByEnter',
  props: {
    placeholder: {
      type: String,
      default: '请选择'
    },
    value: {
      type: String,
      default: ''
    },
    id: {
      type: String,
      require: true
    }
  },
  data() {
    return {
      options: [],
      model: ''
    }
  },
  created() {
    if (this.id) {
      getEnterWarehouse({orderId: this.id}).then(arr => {
        this.options = arr
        let match = false
        let hasEmitChange = false
        if (Array.isArray(this.options)) { // 李其龙要求option有匹配的id才展示
          for (let v of this.options) {
            if (v.value === this.value) {
              this.model = this.value
              match = true
              break
            }
          }
          if (!match) {
            this.change('')
            // this.change('')
            if (Array.isArray((this.options)) && this.options.length > 0) {
              // 默认填写
              hasEmitChange = true
              this.$nextTick(_ => {
                this.change(this.options[0].value)
              })
            }
          }
        }
        if (!hasEmitChange) {
          this.change(this.value)
        }
      })
    }
  },
  watch: {
    value(val, oldVal) {
      if (val === oldVal) {
        return
      }
      this.model = val
    },
    modal(val, oldVal) {
      if (val === oldVal) {
        return
      }
      this.change(val)
    }
  },
  methods: {
    change(val) {
      this.$emit('input', val)
      this.$emit('change', val)
    },
    emitNum(val) {
      this.$emit('input', val)
      this.$emit('change', val)
      this.$emit('update:canUsePackageNum', val)
    }
  }
}
</script>
